#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {  
    my = new MyObject;
    firstButton = new QPushButton(tr("first"), 0);
    connect(firstButton, SIGNAL(clicked()), my, SLOT(first()), Qt::QueuedConnection);
    secondButton = new QPushButton(tr("second"), 0);
    connect(secondButton, SIGNAL(clicked()), my, SLOT(second()), Qt::QueuedConnection);
    threeButton = new QPushButton(tr("three"), 0);
    connect(threeButton, SIGNAL(clicked()), my, SLOT(three()), Qt::QueuedConnection);
    selfButton = new QPushButton(tr("self"), 0);
    connect(selfButton, SIGNAL(clicked()), this, SLOT(onSelfPushed()));
    exitButton = new QPushButton(tr("exit"), 0);
    connect(exitButton, SIGNAL(clicked()), this, SLOT(onExitPushed()));

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(firstButton);
    layout->addWidget(secondButton);
    layout->addWidget(threeButton);
    layout->addWidget(selfButton);
    layout->addWidget(exitButton);

    QWidget *p = new QWidget;
    p->setLayout(layout);

    QThread *thread = new QThread;
    my->moveToThread(thread);

    thread->start();
    connect(thread, SIGNAL(started()), my, SLOT(first()));

    setCentralWidget(p);

    m_pWorker = new Worker;
    m_pWorkerThread = new QThread(this);

    connect(m_pWorkerThread, SIGNAL(started()),m_pWorker, SLOT(doWork()));
    connect(m_pWorkerThread, SIGNAL(finished()), m_pWorker, SLOT(deleteLater()));
    m_pWorker->moveToThread(m_pWorkerThread);
    m_pWorkerThread->start();

/**
如果需要在doWork()函数传递参数中，涉及传递的参数是自定义的类型时，需要将其注册为元类型。
如下的示例，
    1.我将需要计算的数据先进行预处理之后，发送我自定义的geoDataReady信号。
    2.在线程中进行几何计算，完成之后，又将计算完成之后的数据发送回主函数。如下。

    qRegisterMetaType<std::vector<MyGeometry*>>("std::vector<MyGeometry* >");
    //在线程中进行几何运算
    m_pGeoWorker = new CMyWork;
    connect(this, SIGNAL(geoDataReady(const std::vector<MyGeometry> &)), m_pGeoWorker, SLOT(get(const std::vector<MYGeometry* > &)));
    connect(m_pGeoWorker, SIGNAL(geoResultCompleted(const std::vector<MYGeometry* > &)), this, SLOT(draw(const std::vector<MYGeometry* > &)));
    connect(&m_geoThread, SIGNAL(finished()), m_pGeoWorker, SLOT(deleteLater()));
    m_pGeoWorker->moveToThread(&m_geoThread);
    m_geoThread.start();

*/
}  

MainWindow::~MainWindow() {  
    disconnect(m_pWorkerThread, SIGNAL(started()),m_pWorker, SLOT(doWork()));
    disconnect(m_pWorkerThread, SIGNAL(finished()), m_pWorker, SLOT(deleteLater()));
    if (m_pWorkerThread)
    {
        if (m_pWorkerThread->isRunning())
        {
            m_pWorkerThread->quit();
            m_pWorkerThread->wait();
        }
        delete m_pWorkerThread;
        m_pWorkerThread = NULL;
    }
    if (m_pWorker)
    {
        delete m_pWorker;
        m_pWorker = NULL;
    }
}  

void MainWindow::onFirstPushed() {  
    my->first();
}  

void MainWindow::onSecondPushed() {  
    my->second();
}  

void MainWindow::onThreePushed() {  
    my->three();
}  

void MainWindow::onSelfPushed() {  
    qDebug() << QThread::currentThreadId();
}  

void MainWindow::onExitPushed() {  
    close();
} 
